﻿BENLAI.Model.AlertPhoneBind = function () {
    if (!BENLAI.Model.AlertPhoneTemp.NoFirstLoad) {
        $('body').CreateController({
            appEvents: {
                'AlertPhone:Alert': 'init'
            },
            events: {
                '[data-alertType="acode"] click': 'SendSMS',
                '[data-alertType="close"] click': 'CloseAlertPhone',
                '[data-alertType="phone"] .bringin01 keyup': 'RegPhone',
                '[data-alertType="phone"] .bringin01 blur': 'CheckPhone',
                '[data-alertType="phone"] .bringin01 focus': 'DefaultPhone',
                '[data-alertType="code"] .bringin02 focus': 'DefaultCode',
                '[data-alertType="code"] .bringin02 blur': 'CheckCode',
                '[data-alertType="submit"] click': 'Submit'
            },
            bind: function () {
                var that = this;
                this.HideAllMsg();
                this.ajax({
                    url: '/ajax/AlertPhoneTan',
                    success: function (data) {
                        if (data.status > 0) {
                            if (data.data != "") {
                                BENLAI.Model.AlertPhoneTemp.Phone = data.data;
                                BENLAI.Model.AlertPhoneTemp.HasBind = true;
                                BENLAI.Model.AlertPhoneTemp.IsEnable = true;
                                BENLAI.Model.AlertPhoneTemp.FirstOpen = true;
                                that.HasBind();
                            }
                            else {
                                BENLAI.Model.AlertPhoneTemp.HasBind = false;
                                BENLAI.Model.AlertPhoneTemp.IsEnable = false;
                                that.NoBind();
                            }
                        } else {
                            that.NoLogin(data.msg);
                        }
                    }
                });
            },
            DefaultPhone: function (t,event) {
                if ($(t).val() == '请输入手机号')
                    $(t).val('');
            },
            DefaultCode: function (t,event) {
                if ($(t).val() == '请输入验证码')
                    $(t).val('');
            },
            CloseAlertPhone: function () {
                clearTimeout($.BENLAI.countDown.Timer);
                $('[data-alertType="acode"]').remove();
                $('[data-alertType="code"] input').after('<a data-alertType="acode" href="javascript:void(0)">获取验证码</a>');
                $('[data-id="AlertPhoneBind"]').hide();
                HideBack();
            },
            RegPhone: function (t, event) {
                var v = $.trim($(t).val());
                if (v.length >= 11 && !this.regExp.cellPhone.test(v)) {
                    this.ErrorMsg(1);
                    $(t).val(v.substring(0, 11));
                    return false;
                }
            },
            CheckPhone: function () {
                var that = this;
                var phone = $('[data-alertType="phone"] .bringin01').val();
                if (phone == "") {
                    $('[data-alertType="phone"] .bringin01').val('请输入手机号');
                    this.ErrorMsg(0);
                    return false;
                }
                var c = this.regExp.cellPhone;
                if (!c.test(phone)) {
                    this.HideAllMsg();
                    this.ErrorMsg(1);
                    return false;
                }
                this.HideAllMsg();
                this.ajax({
                    url: '/ajax/CheckCanBindPhone',
                    data: { phone: phone },
                    success: function (data) {
                        if (data.status == 1) {
                            that.CanSendsms();
                        } else if (data.status == -2) {
                            that.ErrorMsg(2);
                        } else {
                            that.NoLogin(data.msg);
                        }
                    }
                });
            },
            CheckCode: function () {
                var code = $('[data-alertType="code"] .bringin02').val();
                if (code == "") {
                    $('[data-alertType="code"] .bringin02').val('请输入验证码');
                    this.ErrorMsg(3);
                    return false;
                }
            },
            CanSendsms: function () {
                $('[data-alertType="phone"] [data-yorn="y"]').show();
                $('[data-alerttype="acode"]').removeClass('phone_none');
                BENLAI.Model.AlertPhoneTemp.IsEnable = true;
            },
            HasBind: function () {
                var showPhone = BENLAI.Model.AlertPhoneTemp.Phone.substring(0, 3) + "****" + BENLAI.Model.AlertPhoneTemp.Phone.substring(7, 11);
                $('[data-alertType="title"]').html('手机验证<span>为了您的账户安全，请您验证手机号！</span>');
                $('[data-alertType="bindmsg1"] span').html(showPhone);
                $('[data-alertType="phone"]').hide();
                $('[data-alertType="bindmsg2"]').hide();
                this.SendSMS();
            },
            NoBind: function () {
                $('[data-alertType="title"]').html('绑定手机<span>为了您的账户安全，请立即绑定手机号！</span>');
                $('[data-alertType="acode"]').addClass('phone_none');
                $('[data-alertType="bindmsg1"]').hide();
            },
            NoLogin: function (msg) {
                $('[data-alertType="code"] p').html(msg);
                $('[data-alertType="code"] p').show();
            },
            SendSMS: function (t, event) {
                var that = this;
                if (BENLAI.Model.AlertPhoneTemp.IsEnable) {
                    this.HideAllMsg(1);
                    var phone = "";
                    if (BENLAI.Model.AlertPhoneTemp.HasBind) {
                        phone = BENLAI.Model.AlertPhoneTemp.Phone;
                    } else {
                        phone = $('[data-alertType="phone"] .bringin01').val();
                    }
                    var c = this.regExp.cellPhone;
                    if (!c.test(phone)) {
                        this.ErrorMsg(1);
                        return false;
                    }
                    BENLAI.Model.AlertPhoneTemp.IsEnable = false;
                    this.YanZhengMaRunTime();
                    if (!BENLAI.Model.AlertPhoneTemp.FirstOpen) {
                        this.ajax({
                            url: '/ajax/SendSmsBindPhone',
                            data: { phone: phone },
                            success: function (data) {
                                if (data.status != 1) {
                                    that.NoLogin(data.msg);
                                }
                            }
                        });
                    }
                }
            },
            YanZhengMaRunTime: function () {
                $('[data-alertType="acode"]').addClass('phone_none');
                $('[data-alertType="acode"]').html('');
                var now = new Date();
                var date = new Date(now.getTime() + 60000);
                $.BENLAI.countDown({
                    dom: $('[data-alertType="acode"]'),
                    date: date,
                    temp: '{second}秒后重新获取',
                    callback: function () {
                        BENLAI.Model.AlertPhoneTemp.FirstOpen = false;
                        BENLAI.Model.AlertPhoneTemp.IsEnable = true;
                        $('[data-alertType="acode"]').removeClass('phone_none');
                        $('[data-alertType="acode"]').html('重新获取');
                    }
                });
            },
            HideAllMsg: function (num) {
                $('[data-id="AlertPhoneBind"]').find('p').hide();
                switch (num) {
                    case 1: $('[data-id="AlertPhoneBind"]').find('[data-alertType="ynimg"][data-yorn="n"]').hide(); break;
                    default: $('[data-id="AlertPhoneBind"]').find('[data-alertType="ynimg"]').hide(); break;
                }
            },
            ErrorMsg: function (num) {
                switch (num) {
                    case 0: $('[data-alertType="phone"] [data-yorn="n"]').show(); $('[data-alertType="phone"] p').html('请输入手机号！'); $('[data-alertType="phone"] p').show(); break;
                    case 1: $('[data-alertType="phone"] [data-yorn="n"]').show(); $('[data-alertType="phone"] p').html('手机号码格式不正确，请重新输入！'); $('[data-alertType="phone"] p').show(); break;
                    case 2: $('[data-alertType="phone"] [data-yorn="n"]').show(); $('[data-alertType="phone"] p').html('该手机号已被绑定，请重新输入！'); $('[data-alertType="phone"] p').show(); break;
                    case 3: $('[data-alertType="code"] [data-yorn="n"]').show(); $('[data-alertType="code"] p').html('请输入验证码！'); $('[data-alertType="code"] p').show(); break;
                    case 4: $('[data-alertType="code"] [data-yorn="n"]').show(); $('[data-alertType="code"] p').html('验证码有误！'); $('[data-alertType="code"] p').show(); break;
                    default: this.HideAllMsg(); break;
                }
            },
            Submit: function () {
                var that = this;
                this.HideAllMsg();
                var phone = "";
                if (BENLAI.Model.AlertPhoneTemp.HasBind) {
                    phone = BENLAI.Model.AlertPhoneTemp.Phone;
                } else {
                    phone = $('[data-alertType="phone"] .bringin01').val();
                }
                var code = $('[data-alertType="code"] .bringin02').val();
                if (phone == "" || code == "") {
                    if (phone == "") this.ErrorMsg(0);
                    if (code == "") this.ErrorMsg(3);
                }
                var c = this.regExp.cellPhone;
                if (!c.test(phone)) {
                    this.ErrorMsg(1);
                    return false;
                }
                if (code == "") {
                    this.ErrorMsg(3);
                    return false;
                }
                this.ajax({
                    url: '/ajax/CheckVerifyCode',
                    data: { phone: phone, code: code },
                    success: function (data) {
                        if (data.status == 1) {
                            that.CloseAlertPhone();
                            if (BENLAI.Model.AlertPhoneTemp.nextfun) {
                                BENLAI.Model.AlertPhoneTemp.nextfun();
                            }
                        } else if (data.status == -2) {
                            that.ErrorMsg(4);
                        } else {
                            that.NoLogin(data.msg);
                        }
                    }
                });
            },
            init: function () {
                ShowBack();
                BENLAI.Model.AlertPhoneTemp.NoFirstLoad = true;
                $('[data-alertType="phone"] .bringin01').html('');
                $('[data-alertType="phone"] .bringin02').html('');
                this.bind.apply(this, arguments);
                $('[data-id="AlertPhoneBind"]').show();
            }
        })
    } else {
        $.App.trigger('AlertPhone:Alert');
    }
}
BENLAI.Model.AlertPhoneTemp = {};
